#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QPushButton* myButton = new QPushButton(this);
    myButton->setText("myButton");
    myButton->move(200, 200);

    //Qt5以后，给connect提供重载版本，第二个和第四个参数成为泛型参数，允许传入任意类型的函数指针
    //connect(myButton, &QPushButton::clicked, this, &Widget::close);
    //connect(myButton, &QPushButton::clicked, this, &Widget::handleClicked);

    //使用labmda
    connect(myButton, &QPushButton::clicked, this, &Widget::handleClicked);

    connect(this, &Widget::mySignal, this, &Widget::handleMySignal);
    connect(ui->uibutton2, &QPushButton::clicked, this, [myButton, this](){
        qDebug() << "lambda has done";
        myButton->move(300,300);
        this->move(300,300);
        });



}

Widget::~Widget()
{
    delete ui;
}

void Widget::handleClicked()
{
    //this->setWindowTitle("自定义按钮");
    emit(mySignal("myButton1", "myButton2"));

}

void Widget::handleClickui2()
{
    //1.接触信号和槽的绑定
    disconnect(ui->uibutton2, &QPushButton::clicked, this, &Widget::handleClickui2);
    //2.如果没有解绑，就会构成一个信号绑定了两个槽函数，触发信号的时候，两个槽函数都会执行
    connect(ui->uibutton2, &QPushButton::clicked, this, &Widget::handleClicked);
    emit(mySignal("Clickui2button","ui2"));
}

void Widget::handleMySignal(const QString& text)
{
    //发出自定义信号
    QString s = "来自myButton的参数:" + text;
    this->setWindowTitle(s);
}

//通过函数形式自动完成关联
//函数符合该规则，自动连接信号与槽
//on_(按钮objextName)_(信号名字)
void Widget::on_uiButton_clicked()
{
    //this->setWindowTitle("ui被点击");
    emit(mySignal("clickedui1","ui2"));
}
